//
//  TDUserInfoViewController.m
//  TFB
//
//  Created by yull on 15/8/14.
//  Copyright (c) 2015年 TD. All rights reserved.
//

#import "TDUserInfoViewController.h"

@interface TDUserInfoViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *leftArray;
@property (nonatomic, strong) NSMutableArray *rightArray;

@end

@implementation TDUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationItem.title = @"我的账户";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.leftArray = @[@"手机号码:", @"开户姓名:", @"开户银行:", @"银行卡号:"];
    
    NSString *mobile = [[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE];
    mobile = [NSString stringWithFormat:@"%@****%@", [mobile substringToIndex:3], [mobile substringFromIndex:7]];
    self.rightArray = [NSMutableArray arrayWithArray:@[mobile,
                        [TDUser defaultUser].custName.length ? [TDUser defaultUser].custName : @"",
                        @"--",
                        @"--"]];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.autoresizingMask = self.view.autoresizingMask;
    tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, SVFS.width, 60)];
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    backBtn.frame = CGRectMake(10, 16, SVFS.width - 20, 44);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"bg_btn"] forState:0];
    [backBtn setTitle:@"返回" forState:0];
    [backBtn setTitleColor:[UIColor whiteColor] forState:0];
    [backBtn addTarget:self action:@selector(onClickBackBtnBtn:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:backBtn];
    self.tableView.tableFooterView = footerView;
    
    [self initData];
}

- (void)initData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [TDHttpEngine requestFor_GETCARDINFO_WithCustMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] custId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] complete:^(BOOL succeed, NSString *msg, id reqBody) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if (succeed) {

            NSArray *bankCardList = [reqBody objectForKey:@"bankCardList"];
            if (bankCardList.count) {
                NSDictionary *cardDic = bankCardList.firstObject;
                
                NSString *issnam = [cardDic objectForKey:@"issnam"];
                if ([issnam isKindOfClass:[NSString class]] && issnam.length) {
                    [self.rightArray replaceObjectAtIndex:2 withObject:issnam];
                }
                
                NSString *cardNo = [cardDic objectForKey:@"cardNo"];
                if ([cardNo isKindOfClass:[NSString class]] && cardNo.length) {
                    cardNo = [NSString stringWithFormat:@"%@****%@", [cardNo substringToIndex:6], [cardNo substringFromIndex:cardNo.length - 4]];
                    [self.rightArray replaceObjectAtIndex:3 withObject:cardNo];
                }
                
                [self.tableView reloadData];
            }
        } else {
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}

- (void)onClickBackBtnBtn:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.leftArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SVFS.width, 30)];
    header.backgroundColor = [UIColor whiteColor];
    
    UILabel *leftL = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 20)];
    leftL.textColor = [UIColor colorWithRed:63.0/255.0 green:157.0/255.0 blue:176.0/255.0 alpha:1];
    leftL.font = [UIFont systemFontOfSize:15];
    leftL.text = @"信息确认";
    [header addSubview:leftL];
    
    UILabel *rightL = [[UILabel alloc] initWithFrame:CGRectMake(SVFS.width - 10 - 150, 5, 150, 20)];
    rightL.textAlignment = NSTextAlignmentRight;
    rightL.font = [UIFont systemFontOfSize:15];
    rightL.text = @"请核对商户信息";
    [header addSubview:rightL];
    
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(10, 29, SVFS.width - 20, 1)];
    lineV.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.2];
    [header addSubview:lineV];
    
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SVFS.width, 20)];
    footer.backgroundColor = [UIColor whiteColor];
    
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(10, 1, SVFS.width - 20, 1)];
    lineV.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.2];
    [footer addSubview:lineV];
    
    UILabel *leftL = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 200, 16)];
    leftL.textColor = [UIColor colorWithWhite:0.2 alpha:0.5];
    leftL.font = [UIFont systemFontOfSize:12];
    leftL.text = @"* 每日提现按照以上商户信息清算";
    [footer addSubview:leftL];
    
    return footer;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TransactionQueryIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *leftL = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 80, 20)];
        leftL.font = [UIFont systemFontOfSize:14];
        leftL.textColor = [UIColor grayColor];
        leftL.tag = 1001;
        [cell.contentView addSubview:leftL];
        
        UILabel *rightL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftL.frame), 2, SVFS.width - 10 - CGRectGetMaxX(leftL.frame), 20)];
        rightL.font = [UIFont systemFontOfSize:17];
        rightL.tag = 1002;
        [cell.contentView addSubview:rightL];
    }
    
    UILabel *leftL = (UILabel *)[cell.contentView viewWithTag:1001];
    leftL.text = self.leftArray[indexPath.row];
    
    UILabel *rightL = (UILabel *)[cell.contentView viewWithTag:1002];
    rightL.text = self.rightArray[indexPath.row];
    
    if (indexPath.row == 3) {
        rightL.textColor = [UIColor orangeColor];
    } else {
        rightL.textColor = [UIColor blackColor];
    }
    
    return cell;
}

@end
